#include <gio/gio.h>
#include <gio/giotypes.h>

#include <iostream>
#include <thread>
#include <vector>

#include <kylin-ai/private/osassistant/osassistant.h>

void testNlp() {
   GMainLoop *pMainLoop = g_main_loop_new(nullptr, false);

   std::thread ctrlThread([pMainLoop] {
       g_main_loop_run(pMainLoop);
       g_main_loop_unref(pMainLoop);
   });
   ctrlThread.detach();

   kyai::assistant::OsAssistant oa;
   auto error = oa.init();
   oa.setChatAsyncCallback([](const std::string message) {
       std::fprintf(stdout, "Test nlp message: %s\n", message.c_str());
   });

   oa.chatAsync(R"(
         {
             "message_type": "chat",
             "content": [
                 {"type": "text", "text": "你是谁？"}
             ]
         }
     )");

   while (std::getchar() != '\n') {
   }

   g_main_loop_quit(pMainLoop);
}

void testNlpWithHistory() {
   GMainLoop *pMainLoop = g_main_loop_new(nullptr, false);

   std::thread ctrlThread([pMainLoop] {
       g_main_loop_run(pMainLoop);
       g_main_loop_unref(pMainLoop);
   });
   ctrlThread.detach();

   kyai::assistant::OsAssistant oa;
   oa.setChatAsyncCallback([](const std::string message) {
       std::fprintf(stdout, "Test nlp with history Message: %s\n", message.c_str());
   });

   std::string user = "今天天气很好";
   std::string answer = "今天天气很好”这句话除了直接表述天气状况外，\
           在某些语境下也可能是一种暗示或隐喻，比如可能是某人想要邀请你外出活动，或者是表达一种积极的心情或态度。\
           如果你有关于天气或者如何根据天气安排活动的问题，或者其他任何问题，都可以继续提出.";
   auto error = oa.initWithChatHistory(std::vector<kyai::assistant::Message>{ {user, answer}}, -1 );
   if (error.code != kyai::assistant::ErrorCode::Ok) {
       std::fprintf(stderr, "Init session error: %d:%s\n", (int)error.code, error.message.c_str());
   }

   oa.chatAsync(R"(
         {
             "message_type": "chat",
             "content": [
                 {"type": "text", "text": "重新回答"}
             ]
         }
     )");

   while (std::getchar() != '\n') {
   }

   g_main_loop_quit(pMainLoop);
}

void printPrompt() {
    kyai::assistant::OsAssistant assistant;
    std::vector<kyai::assistant::Prompt> allPrompts = assistant.prompts();
    for (auto item : allPrompts) {
        fprintf(stdout, "get all prompts: %i %s %s %i\n", item.id, item.name.c_str(),
                item.content.c_str(), item.type);
    }
    fprintf(stdout, "print prompt finished\n\n");
}

void testCreatePrompt() {
    kyai::assistant::OsAssistant assistant;

    bool createResult = assistant.createPrompt("chToDongbei", "转换成东北话");
    fprintf(stdout, "createPrompt %i\n", createResult);
}

void testDeletePrompt() {
    kyai::assistant::OsAssistant assistant;

    //确保提示词id存在，才能删除成功
    bool deleteResult = assistant.deletePrompt(101);
    fprintf(stdout, "deleteResult %i\n", deleteResult);
}

void testUpdatePrompt() {
    kyai::assistant::OsAssistant assistant;

    //确保提示词id存在，才能更新成功
    bool updateResult = assistant.updatePrompt(111, "chToShandong", "转换成山东话");
    fprintf(stdout, "updatePrompt %i\n", updateResult);
}

void setPromptOrder() {
    //需要获取所有提示词后，再设置提示词顺序, 保证没有遗漏id
    printPrompt();
    kyai::assistant::OsAssistant assistant;
    std::list promptIdList = {1,3,2,4,6,7,9,8,5,103,102};

    assistant.setPromptOrder(promptIdList);
}

void testPrompt() {
    GMainLoop *pMainLoop = g_main_loop_new(nullptr, false);

    std::thread ctrlThread([pMainLoop] {
        g_main_loop_run(pMainLoop);
        g_main_loop_unref(pMainLoop);
    });
    ctrlThread.detach();

    //获取打印当前所有提示词
    printPrompt();
    //创建提示词,多创建几个方便测试删除和更新
    testCreatePrompt();
    testCreatePrompt();
    testCreatePrompt();
    printPrompt();

    testDeletePrompt();
    printPrompt();

    testUpdatePrompt();
    printPrompt();

//    setPromptOrder();
//    printPrompt();

    while (std::getchar() != '\n') {
    }
    g_main_loop_quit(pMainLoop);
}

int main() {
    testPrompt();
//    testNlp();
//    testNlpWithHistory();

    return 0;
}
